package com.cenwangkeji.resident.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.bean.BannerBean;
import com.cenwangkeji.resident.model.bean.CommunityNoticeBean;
import com.cenwangkeji.resident.model.bean.HealthContentBean;
import com.cenwangkeji.resident.model.bean.HealthTypeBean;
import com.cenwangkeji.resident.model.bean.ServicePackageBean;
import com.cenwangkeji.resident.presenter.fragment.HomePresenter;
import com.cenwangkeji.resident.ui.activity.AssessActivity;
import com.cenwangkeji.resident.ui.activity.CommunityDetailActivity;
import com.cenwangkeji.resident.ui.activity.HealthDetailActivity;
import com.cenwangkeji.resident.ui.activity.MainActivity;
import com.cenwangkeji.resident.ui.activity.MessageActivity;
import com.cenwangkeji.resident.ui.activity.ServicePackageAllActivity;
import com.cenwangkeji.resident.ui.activity.ServicePackageDetailActivity;
import com.cenwangkeji.resident.ui.activity.ServicePackageEvaluateActivity;
import com.cenwangkeji.resident.ui.activity.SignActivity;
import com.cenwangkeji.resident.ui.adapter.CommuntiyNoticeAdapter;
import com.cenwangkeji.resident.ui.adapter.HomeHealthContentAdapter;
import com.cenwangkeji.resident.ui.adapter.HomeHealthTypeAdapter;
import com.cenwangkeji.resident.ui.adapter.ServicePackageAdpter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.base.BaseFragment;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @松 on 2019/10/22 16:40
 * 首页
 */
public class HomeFragment extends BaseFragment implements HomePresenter.I {
    @BindView(R.id.banner)
    Banner mBanner;
    Unbinder unbinder;
    @BindView(R.id.rv_health)
    RecyclerView mRvHealth;
    @BindView(R.id.rv_notice)
    RecyclerView mRvNotice;
    @BindView(R.id.rv_type)
    RecyclerView mRvType;
    @BindView(R.id.rv_health_content)
    RecyclerView mRvHealthContent;
    @BindView(R.id.tv_health)
    TextView mTvHealth;
    private HomePresenter mPresenter = new HomePresenter(this);

    @Override
    public int setLayoutId() {
        return R.layout.fragment_home;
    }


    @Override
    protected void initView(View view) {
        super.initView(view);
        unbinder = ButterKnife.bind(this, view);

    }

    @Override
    protected void initData() {
        super.initData();
        showLoading("正在加载");
        mPresenter.getBanner();//获取轮播图
        mPresenter.getCategory();//获取健康分类
        mPresenter.getNotice();//获取公告
        mPresenter.queryService();//获取用户服务包


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onBanner(BannerBean bannerbean) {
        if (bannerbean.getData() != null) {
            ArrayList<String> list = new ArrayList<>();
            //   设置图片集合
            for (int i = 0; i < bannerbean.getData().size(); i++) {
                list.add(Constant.base + bannerbean.getData().get(i));
            }
            mBanner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with(context).load(path).into(imageView);
                }

            }).setImages(list).setBannerAnimation(Transformer.CubeIn).setDelayTime(3000).start();

        } else {
            MyToast.show(bannerbean.getMsg());
        }
        dismissLoading();

    }

    //健康分类
    @Override
    public void onCategory(final HealthTypeBean bean) {
        if (bean.getData() != null) {
            LinearLayoutManager manager = new LinearLayoutManager(getContext());
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mRvType.setLayoutManager(manager);
            bean.getData().get(0).setIs(true);//默认显示第一个
            mPresenter.getCategoryList(bean.getData().get(0).getId());//请求第一个分类数据
            HomeHealthTypeAdapter adapter = new HomeHealthTypeAdapter(bean.getData());
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    bean.clearSelect();//
                    bean.getData().get(position).setIs(true);
                    adapter.notifyDataSetChanged();
                    mPresenter.getCategoryList(bean.getData().get(position).getId());
                }
            });
            mRvType.setAdapter(adapter);

        } else {
            MyToast.show(bean.getMsg());
        }
    }


    //健康分类内容
    @Override
    public void onCategoryContent(final HealthContentBean bean) {
        if (bean.getData() != null) {
            mRvHealthContent.setLayoutManager(new LinearLayoutManager(getContext()));
            HomeHealthContentAdapter adapter = new HomeHealthContentAdapter(bean.getData());
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(getActivity(), HealthDetailActivity.class);
                    intent.putExtra("id", bean.getData().get(position).getId());
                    startActivity(intent);
                }
            });
            mRvHealthContent.setAdapter(adapter);
        } else {
            MyToast.show(bean.getMsg());
        }
    }

    //公告
    @Override
    public void onNotice(final CommunityNoticeBean bean) {
        if (bean.getData() != null) {
            mRvNotice.setLayoutManager(new LinearLayoutManager(getContext()));
            CommuntiyNoticeAdapter adapter = new CommuntiyNoticeAdapter(bean.getData());
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(getActivity(), CommunityDetailActivity.class);
                    intent.putExtra("id", bean.getData().get(position).getId());
                    startActivity(intent);
                }
            });
            mRvNotice.setAdapter(adapter);
        } else {
            MyToast.show(bean.getMsg());
        }
    }

    //服务包
    @Override
    public void onServicePackage(final ServicePackageBean bean) {
        if (bean.getMsg().equals(Constant.token_expired)) {
            BaseActivity activity = (MainActivity) getActivity();
            activity.signIn();
            activity.finish();
        } else {
            if (bean.getData() !=null&&bean.getData().size() > 0) {
                LinearLayoutManager manager = new LinearLayoutManager(getContext());
                manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                mRvHealth.setLayoutManager(manager);
                ServicePackageAdpter adpter = new ServicePackageAdpter(bean.getData());
                adpter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                           if (bean.getData().get(position).getType()==0){
                               MyToast.show("未制定");
                               MyLogger.i("未制定");
                           }else {
                               Intent intent = new Intent(getActivity(), ServicePackageDetailActivity.class);
                               intent.putExtra("date", bean.getData().get(position));
                               startActivity(intent);
                           }
                    }
                });
                mTvHealth.setVisibility(View.INVISIBLE);
                mRvHealth.setAdapter(adpter);


            } else {
                mTvHealth.setVisibility(View.VISIBLE);
                //MyToast.show("没有制定服务包");
            }
        }
    }


    @OnClick({R.id.iv_signing, R.id.iv_assess, R.id.tv_signing, R.id.tv_assess, R.id.tv_service, R.id.tv_estimate, R.id.iv_service, R.id.iv_estimate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_signing://签约
            case R.id.tv_signing:
                startActivity(new Intent(getActivity(), SignActivity.class));
                break;
            case R.id.iv_assess:
            case R.id.tv_assess://服务包评价
                startActivity(new Intent(getActivity(), ServicePackageEvaluateActivity.class));
                break;
            case R.id.tv_service:
            case R.id.iv_service:
                Intent intent = new Intent(getActivity(), ServicePackageAllActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_estimate://评估
            case R.id.iv_estimate:
                startActivity(new Intent(getActivity(), AssessActivity.class));
                break;
        }
    }


    @OnClick(R.id.iv_message)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), MessageActivity.class));

    }
}
